package com.doform.android.xianggucheng.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.doform.android.xianggucheng.R;
import com.doform.android.xianggucheng.adapter.ArtCateExpListAdapter;
import com.doform.android.xianggucheng.bean.CategoryBean;
import com.doform.android.xianggucheng.retrofit.ApiSpecTransformer.DialogTransformer;
import com.doform.android.xianggucheng.retrofit.subscriber.ErrorHandleSubscriber;
import com.doform.android.xianggucheng.retrofit.HttpManager;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;

/**
 * Created by ph on 2017/6/24.
 */

public class ListCategoryActivity1 extends BaseTitleActivity implements ExpandableListView.OnGroupClickListener, ExpandableListView.OnChildClickListener, OnRefreshListener, OnLoadmoreListener, ArtCateExpListAdapter.OnItemDelete {

    @BindView(R.id.tv_empty)
    TextView mEmpty;

    @BindView(R.id.progressBar)
    ProgressBar mProgressBar;

    @BindView(R.id.elv)
    ExpandableListView mExpListView;

    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;

    private ArtCateExpListAdapter mExpAdapter;

    public static final int PAGE_CONTENT_INIT = 1;
    private int mCurrentPage = PAGE_CONTENT_INIT;

    @Override
    protected int layoutResId() {
        return R.layout.activity_article_category;
    }

    private List<CategoryBean.ListBean> mDatas = new ArrayList<>();

    @Override
    protected void initListener() {
        initListAdapter();
        mExpListView.setOnGroupClickListener(this);
        mExpListView.setOnChildClickListener(this);
        refreshLayout.setEnableHeaderTranslationContent(false);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadmoreListener(this);

    }

    @Override
    protected void showView() {
        titleBar.setTextTitle("商品分类");
    }

    private void initListAdapter() {

        mExpAdapter = new ArtCateExpListAdapter(this, mDatas, mExpListView);
        mExpAdapter.setOnItemClickListener(this);
        mExpListView.setAdapter(mExpAdapter);
    }

    @Override
    protected void updateData() {
        super.updateData();
        mCurrentPage = PAGE_CONTENT_INIT;
        getDataForService(mCurrentPage);
    }

    public void getDataForService(int mCount) {
        HttpManager.instance()
                .getCategory2(String.valueOf(mCount))
                .compose(new DialogTransformer<CategoryBean>(mProgressBar))
                .subscribe(new ErrorHandleSubscriber<CategoryBean>() {
                    @Override
                    public void onNext(@NonNull CategoryBean categoryBean) {
                        notifyData(categoryBean);
                    }
                });
    }

    public void getDataForService2(int mCount) {
        HttpManager.instance()
                .getCategory2(String.valueOf(mCount))
                .subscribe(new ErrorHandleSubscriber<CategoryBean>() {
                    @Override
                    public void onNext(@NonNull CategoryBean categoryBean) {
                        notifyData(categoryBean);
                    }
                });
    }

    private void notifyData(CategoryBean data) {

        List<CategoryBean.ListBean> list = data.getList();

        if (mCurrentPage == PAGE_CONTENT_INIT) {
            mDatas.clear();
        }

        if (mCurrentPage > 1 && (list.size() == 0 || list == null)) {
            mCurrentPage--;
            refreshLayout.finishLoadmore();
            return;
        }
        mDatas.addAll(list);
        mExpAdapter.notifyDataSetChanged();
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadmore();
    }

    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

        TextView tv_name_parent = (TextView) v.findViewById(R.id.tv_name_parent);
        Intent intent = new Intent(this, UpdateArticleCategoryActivity.class);
        intent.putExtra("shangji_name", "-请选择-");
        intent.putExtra("name", tv_name_parent.getText().toString().trim());
        intent.putExtra("id", mDatas.get(groupPosition).getId());
        intent.putExtra("corder", mDatas.get(groupPosition).getCorder());
        startActivity(intent);
        return false;
    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
//        Toast.makeText(getApplicationContext(),"group",Toast.LENGTH_SHORT).show();
        TextView tv_name_child = (TextView) v.findViewById(R.id.tv_name_child);
        Intent intent = new Intent(this, UpdateArticleCategoryActivity.class);
        intent.putExtra("shangji_name", mDatas.get(groupPosition).getClsname());
        intent.putExtra("name", tv_name_child.getText().toString().trim());
        intent.putExtra("id", mDatas.get(groupPosition).get_child().get(childPosition).getId());
        intent.putExtra("corder", mDatas.get(groupPosition).get_child().get(childPosition).getCorder());
        intent.putExtra("fid", mDatas.get(groupPosition).get_child().get(childPosition).getFid());
        startActivity(intent);
        return true;
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        mCurrentPage = PAGE_CONTENT_INIT;
        getDataForService2(mCurrentPage);
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        mCurrentPage++;
        getDataForService2(mCurrentPage);
    }

    @Override
    public void onItemClick(final String id, final String guid) {
        new AlertDialog.Builder(this)
                .setMessage("该数据删除后将无法恢复，请确认是否要删除?")
                .setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                delete(id,guid);
                            }
                        })
                .setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                .show();
    }

    private void delete(String id,String guid) {
        HttpManager.instance()
                .deleteCategory2(id,guid)
                .subscribe(new ErrorHandleSubscriber<Integer>() {
                    @Override
                    public void onNext(@NonNull Integer integer) {
                        mCurrentPage = 1;
                        getDataForService(mCurrentPage);
                    }
                });
    }
    @OnClick(R.id.btn_add)
    public void click() {
        Intent intent = new Intent(this, NewCategoryActivity.class);
        startActivity(intent);
    }
}
